package com.zyx.javademo.nio.channel;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/17 14:55
 */
public class SocketChannelDemo {
    public static void main(String[] args) throws Exception {
        // 创建SocketChannel
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));

        // SocketChannel socketChanne2 = SocketChannel.open();
        // socketChanne2.connect(new InetSocketAddress("www.baidu.com", 80));

        // 设置阻塞和非阻塞
        socketChannel.configureBlocking(false);

        // 读操作
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        socketChannel.read(byteBuffer);
        socketChannel.close();
        System.out.println("read over");

    }
}
